﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace CMS {
    public class Security {
        /*
         * returns true if sender matches data in
         * access token
         */
        public bool IsSenderValid(string username, string hostname, string sessionID) {
            //check if session for sender is open
            if (!isLoggedIn(username))
                return false;

            //check hostname and sessionID match
            Predicate<AuthentificatedUser> userFindPred = new Predicate<AuthentificatedUser>(matchElt);
            tun = username;
            int no = service.authentificatedUsers.FindIndex(userFindPred);

            if (service.authentificatedUsers[no].HostName != hostname)
                return false;

            if (service.authentificatedUsers[no].SessionID != sessionID)
                return false;

            return true;
            
        }
        /*
         * checks if sender matches open session
         */
        private static string tun;

        /*
         * checks if token contains correct data,
         * and if user is logged in
         * and if session data matches with
         * if both true, 
         * returns true
         */
        public static bool IsSenderValid(APIAccessToken token) {

            if (!IsAPIAccessTokenValid(token))
                return false;
            //check if session for sender is open
            if(!isLoggedIn(token.username))
                return false;
            
            //check hostname and sessionID match
            Predicate<AuthentificatedUser> userFindPred = new Predicate<AuthentificatedUser>(matchElt);
            tun = token.username;
            int no = service.authentificatedUsers.FindIndex(userFindPred);

            if (service.authentificatedUsers[no].HostName != token.hostname)
                return false;

            if (service.authentificatedUsers[no].SessionID != token.sessionID)
                return false;

            return true;
        }

        private static bool matchElt(AuthentificatedUser u) {
            return u.UserName == tun;
        }

        /* input validation of access token data
         */
        public static bool IsAPIAccessTokenValid(APIAccessToken token) {
            if (!CMS_input_validation.isUsername(token.username))
                return false;
            if (!CMS_input_validation.isHostname(token.hostname))
                return false;
            if (!CMS_input_validation.isSessionID(token.sessionID))
                return false;
            
            //if all data is correct
            return true;
            
        }
        public bool IsConsumerValid() {

            return true;
        }

        public static bool isLoggedIn(AuthentificatedUser pretender) {
            foreach (AuthentificatedUser us in service.authentificatedUsers) {
                if (pretender.UserName == us.UserName) {
                    //isExpired(us);
                    return true;
                }
            }
            return false;
        }

        public static bool isChangeManager(string pretender) {
            foreach (string cm in service.accessRights.changeManagers) {
                if (pretender == cm)
                    return true;
                break;
            }
            return false;
        }
        
        public static bool isAdviceBoard(string pretender) {
            foreach (string ab in service.accessRights.adviceBoard) {
                if (pretender == ab)
                    return true;
                break;
            }
            return false;
        }

        public static bool isChangeImplementer(string pretender){
            foreach (string ab in service.accessRights.changeImplementers) {
                if (pretender == ab)
                    return true;
                break;
            }
            return false;
        }

        public static bool isEquipmentManager(string pretender) {
            foreach (string ab in service.accessRights.equipmentManagers) {
                if (pretender == ab)
                    return true;
                break;
            }
            return false;
        }

        

        public static bool isLoggedIn(string username) {
            foreach (AuthentificatedUser us in service.authentificatedUsers) {
                if (username == us.UserName) {
                    //isExpired(us);
                    return true;
                }
            }
            return false;
        }

        public static bool isAdviceBoard(AuthentificatedUser pretender)
        {
            foreach (string ab in service.accessRights.adviceBoard)
            {
                if (pretender.UserName == ab)
                    return true;
                break;
            }
            return false;
        }

        /*
         * check if user is a service admin
         */
        public static bool isServiceAdministrator(string pretender)
        {
            foreach (string adm in service.accessRights.serviceAdmins)
            {
                if (pretender == adm){
                    return true;                    
                }
            }
            return false;
        }


        /*
         * returns true, if user is expired, i.e.
         * his last interaction time is too long
         */
        /*
        private bool isExpired(User us)
        {
            DateTime expTime = new DateTime();
            expTime = DateTime.Now; 
            expTime = expTime - us.LastInteraction;
            int estSecs = expTime.Second * expTime.Minute * expTime.Hour;
            if (estSecs > cmsSetts.idleTimeAllowed)
                return true;
            else
                return false;            
        }*/


        public static serviceGroup detectServiceGroup(string username) {
            serviceGroup result;
            if (isServiceAdministrator(username))
                result = serviceGroup.ADMIN;
            else 
                result = serviceGroup.USER;
            return result;
        }

        public static cmsRoleGroup[] detectCMSGroup(string username) {
            cmsRoleGroup[] result = new cmsRoleGroup[5];
            result[0]= cmsRoleGroup.CI;
            if (isChangeManager(username))
                result[1] = cmsRoleGroup.CM;
            if (isAdviceBoard(username))
                result[2] =  cmsRoleGroup.AB;
            if (isChangeImplementer(username))
                result[3] = cmsRoleGroup.CIM;
            if (isEquipmentManager(username))
                result[4]  =  cmsRoleGroup.EM;

            return result;
        }
    }
}
